const fs = require("fs");
const https = require("https");
const path = require("path");

const data = [
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-9/1599653309621-1.1标题.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-9/1599653309622-1.2无序列表.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-9/1599653309623-1.3有序列表.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-25/1601015511898-image.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-9/1599653309625-1.5粗体和斜体.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-25/1601015877358-image.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-9/1599653309626-1.7分割线.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-9/1599653309627-1.8删除线.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-9/1599653309628-1.9表格.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-9/1599653309629-1.10图片.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-9/1599653309632-2.1脚注.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-25/1601015700784-image.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-9/1599653309648-2.3数学公式.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-9/1599653309650-2.4TOC.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-9/1599653309651-2.5注音符号.png"},
  {cover: "https://gitee.com/guanpengchn/picture/raw/master/2020-9-9/1599653309652-2.6横屏滑动幻灯片.png"},
];

// const data = [
//   {
//     cover: "https://files.mdnice.com/pic/ac71ef9d-8e18-42dd-82ba-d223881a5fb8.png",
//     name: "重影",
//     id: 3060,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/46bc9201-ab76-4d54-9fa6-127869a5ba94.png",
//     name: "丘比特忙",
//     id: 3050,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/1a39d465-fc26-4008-901c-5a6b6ba12a45.png",
//     name: "奇点",
//     id: 1377,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/af325a49-1e79-4a50-be03-ea24a4d88720.png",
//     name: "雁栖湖",
//     id: 1348,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/9322d897-85d5-4be1-9c9d-c392d4d39bff.png",
//     name: "橙心",
//     id: 1,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/e989865f-32c6-4201-a486-9f85fe7c2fab.png",
//     name: "姹紫",
//     id: 3,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/94cff81b-a086-46aa-a70e-1b9771b67cfc.png",
//     name: "嫩青",
//     id: 4,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/02f3f593-3380-4aa6-9d90-6e2b6f276558.png",
//     name: "绿意",
//     id: 5,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/9f008265-8511-4f9c-aafd-87027c94bd58.png",
//     name: "红绯",
//     id: 6,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/248727f4-d81c-42a3-9723-3a1e5bdbda30.png",
//     name: "蓝莹",
//     id: 8,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/c65c74bf-37c7-4b0b-97b9-1c8c47615535.png",
//     name: "兰青",
//     id: 10,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/2d041c6f-564b-4392-9636-9998f2b056bb.png",
//     name: "山吹",
//     id: 11,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/473ace64-588c-4bcb-9ef3-0b005c78c1f2.png",
//     name: "前端之巅同款",
//     id: 12,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/d160f5ec-26cb-4de8-8509-a8b29e8dd20c.png",
//     name: "极客黑\n\n",
//     id: 13,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/06aa2d77-0a16-44c4-93f2-3aff4c67e095.png",
//     name: "蔷薇紫",
//     id: 15,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/168a4408-be49-4706-826f-b63c001f8ba3.png",
//     name: "萌绿",
//     id: 16,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/39cc0084-e0ca-47e3-b661-6c00c61c23b5.png",
//     name: "全栈蓝",
//     id: 17,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/d4e91acf-c904-4f2d-9ddd-a6bd275bc54b.png",
//     name: "极简黑",
//     id: 18,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/31204a5a-254e-487e-879d-4546ebcce3c3.jpg",
//     name: "橙蓝风",
//     id: 19,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/87c1226f-0788-4d26-8a69-e7be82f70932.png",
//     name: "Pornhub黄",
//     id: 33,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/73c130f0-a866-45f0-8d96-ecb3bb33da32.jpg",
//     name: "凝夜紫",
//     id: 35,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/d3b2bc3d-c2a2-4a0f-9c34-931e61af1579.png",
//     name: "萌粉",
//     id: 42,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/1b3a6388-8338-430a-9b6c-f7bb84b3e718.png",
//     name: "Obsidian",
//     id: 44,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/31016bf3-0a07-4ebe-8ded-9999a714ea90.png",
//     name: "灵动蓝",
//     id: 45,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/37bae771-dfed-49d0-bda7-21aed462acc2.jpg",
//     name: "草原绿",
//     id: 48,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/1919fe6a-4e84-4365-bd94-eb66e592c04a.png",
//     name: "科技蓝",
//     id: 51,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/7c426155-27e8-493b-a483-ce966884a8b9.png",
//     name: "WeFormat",
//     id: 62,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/0cf8e3fd-fb8a-4b29-a2d9-d729bc424392.png",
//     name: "简",
//     id: 63,
//   },
//   {
//     cover: "https://files.mdnice.com/pic/c781d9a9-6116-4f60-9eac-f2132844b73d.png",
//     name: "锤子便签主题第2版",
//     id: 1653,
//   },
//   {
//     cover: 'https://files.mdnice.com/pic/d1e2129b-30a8-443d-8ebb-a3f7aa09141e.png'
//   },
//   {
//     cover: 'https://files.mdnice.com/pic/4e116911-86c9-40c7-80ec-bd05e65efa5b.png',
//   },
//   {
//     cover: 'https://files.mdnice.com/pic/c6dd0d41-e95d-4d0d-a202-afa9ca0731af.png'
//   }
// ];

function downloadImage(url) {
  return new Promise((resolve, reject) => {
    const options = {
      headers: {
        // Referer: "https://www.mdnice.com/",
        Referer: "https://gitee.com/",
      },
    };

    https
      .get(url, options, (response) => {
        const fileName = url.split("/").pop();
        const filePath = path.join(__dirname, "images/tutorails", fileName);
        const fileStream = fs.createWriteStream(filePath);
        response.pipe(fileStream);

        fileStream.on("finish", () => {
          fileStream.close();
          resolve();
        });
      })
      .on("error", (error) => {
        reject(error);
        // fs.unlink(filePath, () => {
        //   reject(error);
        // });
      });
  });
}

function downloadAllImages() {
  const downloadPromises = data.map((item) =>
    downloadImage(item.cover)
      .then(() => {
        console.log(`Downloaded: ${item.name}`);
      })
      .catch((error) => {
        console.error(`Failed to download: ${item.name}`, error);
      }),
  );

  return Promise.all(downloadPromises);
}

console.log(11);

downloadAllImages()
  .then(() => {
    console.log("All images downloaded.");
  })
  .catch((error) => {
    console.error("Failed to download all images.", error);
  });
